package com.cdu.ai_intelligent_teaching_and_training_system.service;

import com.cdu.ai_intelligent_teaching_and_training_system.entity.Assignment;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.AssignmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class AssignmentService {
    
    @Autowired
    private AssignmentMapper assignmentMapper;
    
    public List<Assignment> findAll() {
        return assignmentMapper.selectAll();
    }
    
    public Optional<Assignment> findById(Integer id) {
        return Optional.ofNullable(assignmentMapper.selectById(id));
    }
    
    public List<Assignment> findByCourseId(Integer courseId) {
        return assignmentMapper.selectByCourseId(courseId);
    }
    
    // 需要在Mapper.xml中自定义逾期作业和模糊查询
    public List<Assignment> findOverdueAssignments() {
        return assignmentMapper.selectByDueDateBefore(LocalDateTime.now());
    }
    
    public List<Assignment> findByTitleContaining(String title) {
        return assignmentMapper.selectByTitleLike("%" + title + "%");
    }
    
    public Assignment save(Assignment assignment) {
        if (assignment.getAssignmentId() == null) {
            assignmentMapper.insert(assignment);
        } else {
            assignmentMapper.update(assignment);
        }
        return assignment;
    }
    
    public void deleteById(Integer id) {
        assignmentMapper.deleteById(id);
    }
} 